package xin.marcher.module.diplomacy.test.mapper;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class ForJmeterCreateParameterFile {

    public static final Range SKU_INFO_SKU_ID_RANGE = new Range(8_000_000_001L, 8_000_010_000L);

    public static final Range ITEM_INFO_ITEM_ID_RANGE = new Range(100_000_000_001L, 100_000_001_000L);

    public static final Range SELLER_GROUP_ID_RANGE = new Range(300_001, 301_000);

    public static final Range SELLER_INFO_ID_RANGE = new Range(20_000_001, 20_001_000);

    @Test
    public void getFrontCategory() {
        StringBuilder sb = new StringBuilder();
        sb.append("frontCategoryId").append("\n");
        for (Long param : ForJmeterCreateMockData.FRONT_CATEGORY_ID_RANGE.asList()) {
            sb.append(param).append("\n");
        }
        sb.setLength(sb.length() - 1);

        writeToFile(sb.toString(), "getFrontCategory.txt");
    }

    @Test
    public void getFrontCategorySkuList() {
        StringBuilder sb = new StringBuilder();
        sb.append("frontCategoryId").append("\n");
        for (Long param : ForJmeterCreateMockData.FRONT_CATEGORY_ID_RANGE.asList()) {
            sb.append(param).append("\n");
        }
        sb.setLength(sb.length() - 1);

        writeToFile(sb.toString(), "getFrontCategorySkuList.txt");
    }

    @Test
    public void getProductCollect() {
        StringBuilder sb = new StringBuilder();
        sb.append("itemId,skuId").append("\n");
        for (Long param : ForJmeterCreateMockData.ITEM_INFO_ITEM_ID_RANGE.asList()) {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                list.add(ForJmeterCreateMockData.ITEM_INFO_ITEM_ID_RANGE.randomOne().toString());
                list.add(ForJmeterCreateMockData.SKU_INFO_SKU_ID_RANGE.randomOne().toString());
            }
            sb.append(String.join(",", list)).append("\n");
        }
        sb.setLength(sb.length() - 1);

        writeToFile(sb.toString(), "getProductCollect.txt");
    }

    @Test
    public void getProductDetail() {
        StringBuilder sb = new StringBuilder();
        sb.append("skuId").append("\n");
        for (Long skuId : ForJmeterCreateMockData.SKU_INFO_SKU_ID_RANGE.asList()) {
            sb.append(skuId).append("\n");
        }
        sb.setLength(sb.length() - 1);

        writeToFile(sb.toString(), "getProductDetail.txt");
    }

    @Test
    public void listLabels() {
        StringBuilder sb = new StringBuilder();
        sb.append("frontCategoryId").append("\n");
        for (Long prama : ForJmeterCreateMockData.FRONT_CATEGORY_ID_RANGE.asList()) {
            sb.append(prama).append("\n");
        }
        sb.setLength(sb.length() - 1);

        writeToFile(sb.toString(), "listLabels.txt");
    }

    @Test
    public void listProducts() {
        StringBuilder sb = new StringBuilder();
        sb.append("labelId").append("\n");
        for (Long prama : ForJmeterCreateMockData.LABEL_ID_RANGE.asList()) {
            sb.append(prama).append("\n");
        }
        sb.setLength(sb.length() - 1);

        writeToFile(sb.toString(), "listProducts.txt");
    }

    @Test
    public void getSaleSkuBySellerId() {
        StringBuilder sb = new StringBuilder();
        sb.append("sellerId").append("\n");
        for (Long prama : SELLER_INFO_ID_RANGE.asList()) {
            sb.append(prama).append("\n");
        }
        sb.setLength(sb.length() - 1);

        writeToFile(sb.toString(), "getSaleSkuBySellerId.txt");
    }

    @Test
    public void checkSaleSku() {
        StringBuilder sb = new StringBuilder();
        sb.append("sellerId,skuId").append("\n");
        for (Long param : SELLER_INFO_ID_RANGE.asList()) {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                list.add(SELLER_INFO_ID_RANGE.randomOne().toString());
            }
            for (int i = 0; i < 4; i++) {
                list.add(ForJmeterCreateMockData.SKU_INFO_SKU_ID_RANGE.randomOne().toString());
            }
            sb.append(String.join(",", list)).append("\n");
        }
        sb.setLength(sb.length() - 1);

        writeToFile(sb.toString(), "checkSaleSku.txt");
    }

    private void writeToFile(String str, String fileName) {
        try {
            URL classpath = ClassLoader.getSystemResource("");
            String paramFilePath = classpath.getFile() + File.separator + fileName;
            FileWriter fileWriter = new FileWriter(paramFilePath);
            fileWriter.write(str);
            fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Range {
        // 前闭
        private long start;
        // 后也闭
        private long end;

        public List<Long> asList() {
            List<Long> list = new ArrayList<>();
            for (long i = start; i <= end; i++) {
                list.add(i);
            }
            return list;
        }


        public Long randomOne() {
            return RandomUtils.nextLong(start, end + 1);
        }
    }


}
